package com.team7.happycommunity.propertymanagement;

import com.team7.happycommunity.propertymanagement.filter.RegistorFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.ArrayList;

@SpringBootApplication
@MapperScan(value = {"com.team7.happycommunity.propertymanagement.dao"})
@EnableEurekaClient
public class PropertymanagementApplication {

    public static void main(String[] args) {
        SpringApplication.run(PropertymanagementApplication.class, args);
    }

    @Bean
    public RedisCacheConfiguration redisCacheConfiguration(){
        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
        configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        return configuration;
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory){
        RedisTemplate template = new RedisTemplate();
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(factory);
        return template;
    }
//    @Autowired
//    RegistorFilter registorFilter;


//    /**
//     * 配置拦截未登录请求
//     * @return
//     */
//    @Bean
//    public FilterRegistrationBean filterRegistrationBean(){
//        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//        registrationBean.setFilter(registorFilter);
//        ArrayList<String> urls = new ArrayList<>();
//        // 设置验证url
//        urls.add("/user/test");
//        urls.add("/user/test2");
//        registrationBean.setUrlPatterns(urls);
//        return registrationBean;
//    }

}
